//
//  KZFilterView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/7.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZFilterView.h"

@implementation KZFilterView
{
    UIView *_contentView;           // 内容视图
    
    NSMutableArray *_selectedTags;  // 选中的标签tag
}

- (void)dealloc {
    NSLog(@"销毁了");
}

- (instancetype)init {
    if (self = [super init]) {
        
        _selectedTags = @[@0, @0, @0, @0].mutableCopy;
        [self addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)setupUI {
    
    _contentView = [UIView new];
    _contentView.layer.masksToBounds = true;
    _contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
    }];
    NSArray *titles = @[@"复习阶段", @"完成情况", @"作答时间", @"分数段"];
    NSArray *filters = @[@[@"系统复习", @"巩固复习", @"热点模拟"],
                         @[@"全部", @"已完成", @"未完成"],
                         @[@"7天之内", @"2周之内", @"1个月之内", @"1个月以上"],
                         @[@"90分以上", @"75-89分", @"60-74分", @"60分以下"],];
    UIView *last = nil;
    for (int i = 0; i < titles.count; i ++) {
        NSString *title = titles[i];
        NSArray *subfilters = filters[i];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = title;
        titleLabel.font = FONT(15);
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.textColor = [UIColor blackColor];
        [_contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(W(15));
            if (last) {
                make.top.equalTo(last.mas_bottom).offset(H(10));
            } else {
                make.top.mas_equalTo(H(10));
            }
        }];
        
        UIView *filterOptionView = [UIView new];
        [_contentView addSubview:filterOptionView];
        [filterOptionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel);
            make.right.mas_equalTo(W(-15));
            make.top.equalTo(titleLabel.mas_bottom).offset(H(5));
            make.height.mas_equalTo(H(25));
        }];
        last = filterOptionView;
        
        UIView *optionLast = nil;
        for (int k = 0; k < subfilters.count; k ++) {
            
            UIButton *optionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            optionBtn.tag = 10 * (i + 1) + k;
            optionBtn.layer.cornerRadius = 2;
            optionBtn.layer.masksToBounds = true;
            optionBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
            optionBtn.layer.borderWidth = 1;
            optionBtn.titleLabel.font = FONT(14);
            [optionBtn setTitle:subfilters[k] forState:UIControlStateNormal];
            [optionBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
            [optionBtn addTarget:self action:@selector(onTouchOption:) forControlEvents:UIControlEventTouchUpInside];
            [filterOptionView addSubview:optionBtn];
            [optionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo((SCREEN_WIDTH - W(30) - W(3) * 3)/4);
                make.top.height.equalTo(filterOptionView);
                if (optionLast) {
                    make.left.equalTo(optionLast.mas_right).offset(W(3));
                } else {
                    make.left.mas_equalTo(0);
                }
            }];
            optionLast = optionBtn;
        }
    }
    
    if (_lateseSelectedTags != nil) {
        _selectedTags = _lateseSelectedTags.mutableCopy;
        for (int i = 0; i < _selectedTags.count; i ++) {
            NSInteger selectedTag = [_selectedTags[i] integerValue];
            if (selectedTag != 0) {
                UIButton *button = [self viewWithTag:selectedTag];
                button.layer.borderColor = kRedColor.CGColor;
                [button setTitleColor:kRedColor forState:UIControlStateNormal];
            }
        }
    }
    
    UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    resetButton.titleLabel.font = FONT(15);
    resetButton.layer.masksToBounds = true;
    resetButton.layer.cornerRadius = 4;
    resetButton.backgroundColor = UIColor.lightGrayColor;
    [resetButton setTitle:@"重置" forState:UIControlStateNormal];
    [resetButton setTitleColor:kBlueColor forState:UIControlStateNormal];
    [resetButton addTarget:self action:@selector(onTouchReset) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:resetButton];
    [resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->_contentView.mas_centerX).offset(W(-1.5));
        make.height.mas_equalTo(H(25));
        make.width.mas_equalTo(W(120));
        make.top.equalTo(last.mas_bottom).offset(H(30));
    }];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton.titleLabel.font = FONT(15);
    confirmButton.layer.masksToBounds = true;
    confirmButton.layer.cornerRadius = 4;
    confirmButton.backgroundColor = kBlueColor;
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(onTouchConfirm) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_contentView.mas_centerX).offset(W(1.5));
        make.height.width.top.equalTo(resetButton);
    }];
}

#pragma mark - 事件
- (void)onTouchOption:(UIButton *)button {
    
    // 选项索引
    NSInteger i = button.tag / 10 - 1;
    // 之前选中的tag
    NSInteger selectedTag = [_selectedTags[i] integerValue];
    // 取消选中
    if (button.tag == selectedTag) {
        button.layer.borderColor = UIColor.lightGrayColor.CGColor;
        [button setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
        _selectedTags[i] = @0;
    }
    // 选中
    else {
        button.layer.borderColor = kRedColor.CGColor;
        [button setTitleColor:kRedColor forState:UIControlStateNormal];
        
        if (selectedTag != 0) {
            UIButton *prevButton = [self viewWithTag:selectedTag];
            prevButton.layer.borderColor = UIColor.lightGrayColor.CGColor;
            [prevButton setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
        }
        
        _selectedTags[i] = @(button.tag);
    }
    
    
    // 全部未选中则设为“是重置”
    __block BOOL isReset = true;
    [_selectedTags enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj integerValue] != 0) {
            isReset = false;
            *stop = true;
        }
    }];
}
// 重置
- (void)onTouchReset {

    for (int i = 0; i < _selectedTags.count; i ++) {
        NSInteger selectedTag = [_selectedTags[i] integerValue];
        if (selectedTag != 0) {
            UIButton *prevButton = [self viewWithTag:selectedTag];
            prevButton.layer.borderColor = UIColor.lightGrayColor.CGColor;
            [prevButton setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
            _selectedTags[i]= @0;
        }
    }
}
- (void)onTouchConfirm {
    
    // 全部未选中则设为“是重置”
    __block BOOL isReset = true;
    [_selectedTags enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj integerValue] != 0) {
            isReset = false;
            *stop = true;
        }
    }];
    if (self.onConfirmCallback) {
        if (isReset) {
            self.onConfirmCallback(nil);
        } else {
            self.onConfirmCallback(_selectedTags.copy);
        }
        
    }
    [self hide];
}


#pragma mark - 显示与隐藏
- (void)showAtY:(CGFloat)y basalView:(UIView *)view {
    
    ((UIScrollView *)[view viewWithTag:666]).scrollEnabled = false;
    
    self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(y);
        make.left.right.equalTo(view);
        make.bottom.equalTo(view);
    }];
    [self setupUI];
    [self layoutIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];
        
        [self->_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(H(300));
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    
    ((UIScrollView *)[self.superview viewWithTag:666]).scrollEnabled = true;

    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        
        [self->_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
